import { IContext, IParam } from '@/core';
import type { PropType } from 'vue';
interface PagingOption {
	defaultPageSize?: number;
	position?:
		| 'topLeft'
		| 'topCenter'
		| 'topRight'
		| 'bottomLeft'
		| 'bottomCenter'
		| 'bottomRight';
	pageSizeOptions?: string[];
}
//  行激活模式 0:不激活 ,1:单击激活,2:双击激活
declare type rowActiveMode = 0 | 1 | 2;

export const gridProps = {
    localMode: {
        type: Boolean,
        default: false,
    },
	enableAgg: {
		type: Boolean,
		default: false,
	},
	aggData: {
		type: Object,
		default: {},
	},
	showHeader: {
		type: Boolean,
		default: true,
	},
	singleSelect: {
		type: Boolean,
		default: false,
	},
	enableRowEdit: {
		type: Boolean,
		default: false,
	},
	columns: {
		type: Array as PropType<ColumnType[]>,
		default: () => [],
	},
	items: {
		type: Array as PropType<any[]>,
		default: () => [],
	},
	selections: {
		type: Array as PropType<IParam[]>,
		default: () => [],
	},
	paging: {
		type: Object as PropType<PagingOption | null>,
		default: () => null,
	},
	current: {
		type: Number,
		default: 1,
	},
	limit: {
		type: Number,
		default: 20,
	},
	totalRecord: {
		type: Number,
		default: 0,
	},
	groupOptions: {
		type: Object as PropType<IParam | null>,
		default: () => null,
	},
	emptyText: {
		type: String,
		default: '',
	},
	loading: {
		type: Boolean,
		default: false,
	},
	rowActiveMode: {
		type: Number as PropType<rowActiveMode>,
		default: 2,
	},
	context: {
		type: Object as PropType<IContext>,
		default: {},
	},
	viewParams: {
		type: Object as PropType<IParam>,
		default: {},
	},
	data: {
		type: Object as PropType<IParam>,
		default: {},
	},
	rowClassNameLogic: {
		type: Function,
	},
	enableColumnShowChange: {
		type: Boolean,
		default: false
	},
	customCallBack:{
		type: Function,
	},
    columnFilter: {
        type: Object as PropType<IParam>,
		default: {},
    }
};

export const gridColumnProps = {
	text: {
		type: [String, Number, Object],
		default: '',
	},
	column: {
		type: Object as PropType<ColumnType>,
		default: {},
	},
	index: {
		type: Number,
	},
	record: {
		type: Object as PropType<IParam>,
		default: {},
	},
	context: {
		type: Object as PropType<IContext>,
		default: {},
	},
	viewParams: {
		type: Object as PropType<IParam>,
		default: {},
	},
};

export const resizeColumn = {
	caption: '',
	columnType: 'EMPTYCOLUMN',
	dataIndex: 'autowidthcolumn',
	align: 'center',
	ellipsis: true,
	aggMode: 'NONE',
};

export interface ColumnType {
	/**
	 * 列标识
	 *
	 * @type {string}
	 * @memberof ColumnType
	 */
	codeName: string;
	/**
	 * 列样式名
	 *
	 * @type {string}
	 * @memberof ColumnType
	 */
	cssClass?: string;
	/**
	 * 列名称
	 *
	 * @type {string}
	 * @memberof ColumnType
	 */
	name: string;

	/**
	 * 是否开启行编辑
	 *
	 * @type {boolean}
	 * @memberof ColumnType
	 */
	enableRowEdit?: boolean;
	caption: string;
	captionLanResTag?: string;
	width: number;
	widthUnit?: string;
	show: boolean;
	resizable: boolean;
	dataIndex: string;
	headerCssName?: string;
	cellCssName?: string;
	columnType: string | 'DEFGRIDCOLUMN' | 'UAGRIDCOLUMN' | 'GROUPGRIDCOLUMN';
	uIActionDetails?: IParam[];
	uIAction?: IParam;
	codeListTag?: IParam;
	[key: string]: any;
}

export function getColumnTotalWidth(columns: ColumnType[]): number {
	let total = 0;
	columns.forEach((column: ColumnType) => {
		if (column.show) {
			total += column.width ? column.width : 150;
		}
	});
	return total;
}

export function initPaging(opt: IParam): IParam {
	const { paging, current, totalRecord } = opt;
	const option = {
		current,
		defaultPageSize: 20,
		total: totalRecord,
		position: ['bottomRight'],
		showSizeChanger: true,
		pageSizeOptions: ['10', '20', '50', '100'],
		showTotal: (total: number, range: IParam[]) => {
			return `${App.ts('widget.grid.show')} ${range[0]} - ${Math.min(
				Number(range[1]),
				totalRecord
			)} ${App.ts('widget.grid.piece')}，${App.ts(
				'widget.grid.total'
			)} ${totalRecord} ${App.ts('widget.grid.piece')}`;
		},
		buildOptionText: ({ value }: { value: string }) =>
			`${value}${App.ts('widget.grid.page')}`,
	};
	if (paging) {
		Object.assign(option, paging);
	}
	return option;
}
